﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using IQKS.AWP.BusinessAccessLayer;
using IQKS.AWP.BusinessObject;
using SFA;
using System.Configuration;
using System.IO;

public partial class CandidateDetailsReg : System.Web.UI.Page
{

    public string DocumentPath = ConfigurationSettings.AppSettings["ProfilePic"];
    public string CVPath = ConfigurationSettings.AppSettings["CvPath"];

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

            int cid = Convert.ToInt32(CommonUtility.Decrypt(Convert.ToString(Request.QueryString["CandidateID"]), true));
            trModAss.Visible = false;
            trModAssFee.Visible = false;
            trRLAss.Visible = false;
            trRLAssFee.Visible = false;
            trAuditor.Visible = false;
            trAuditorFee.Visible = false;
            trIExpert.Visible = false;
            trIExpertFee.Visible = false;


            GetCandidateDetailsDataSet(cid);
            //GetCandidatePaymentDetails(cid);
            mvwCandidateReg.ActiveViewIndex = 0;
            mainMenu_MenuItemClick(sender, null);
        }
    }

    public void GetCandidateData(DataTable DtCandidateData)
    {
        tdAccreditationType.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["AccrediationType"]);
        // tdCandidateType.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["CandidateType"]);
        string strCandidateType = Convert.ToString(DtCandidateData.Rows[0]["CandidateType"]);
        tdCandidateType.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["CandidateType"]);
        if (strCandidateType == "Independent")
        {
            trMarketReserachCompany.Visible = false;
        }
        else
        {
            trMarketReserachCompany.Visible = true;
            tdMarketReserachCompany.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["CompanyName"]);
        }
        // tdCompanyName.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["CompanyName"]);
        tdEmail.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["Email"]);
        tdFirstName.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["FirstName"]);
        // tdMiddleName.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["MiddleName"]);

        string middlename = Convert.ToString(DtCandidateData.Rows[0]["MiddleName"]);
        if (middlename == "")
        {
            tdMiddleName.InnerHtml = "&nbsp;&nbsp;-";

        }
        else
        {
            tdMiddleName.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["MiddleName"]);

        }

        tdLastName.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["LastName"]);
        //string strDOB = Convert.ToString(DtCandidateData.Rows[0]["DOB"]);
        //string dateOnlyString = strDOB.ToShortDateString();
        tdDOB.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["conDOB"]);
        string strGender = Convert.ToString(DtCandidateData.Rows[0]["Gender"]);
        //if (strGender == "M")
        //{
        //    tdGender.InnerHtml = "Male";
        //}
        //else
        //{
        //    tdGender.InnerHtml = "Female";
        //}
        //tdGender.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["Gender"]);
        //tdNationalityID.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["NationalityID"]);
        tdNationality.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["NationalityName"]);
        tdContectAddress.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["ContactAddress"]);
        tdCountry.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["CountryName"]);
        string tdstatecheck = Convert.ToString(DtCandidateData.Rows[0]["StateName"]);

        if (tdstatecheck == "")
        { tdState.InnerHtml = "&nbsp;&nbsp;-"; }
        else
        {
            tdState.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["StateName"]);
        }
        tdZipCode.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["ZipCode"]);


        //tdHomePhoneNo.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["OtherPhoneNo"]);
        //tdWorkPhoneNo.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["PhoneNo"]);
        //tdMobileNo.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["MobileNo"]);

        string otherphoneno = Convert.ToString(DtCandidateData.Rows[0]["OtherPhoneNo"]);
        string[] otherphonenoparts = otherphoneno.Split('-');
        if (otherphonenoparts[0] == "" || otherphonenoparts[1] == "" || otherphonenoparts[2] == "")
        { tdHomePhoneNo.InnerHtml = "&nbsp;&nbsp;-"; }
        else
        {
            tdHomePhoneNo.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["OtherPhoneNo"]);

        }

        // tdWorkPhoneNo.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["PhoneNo"]);

        string phoneno = Convert.ToString(DtCandidateData.Rows[0]["PhoneNo"]);
        string[] phonenoparts = phoneno.Split('-');
        if (phonenoparts[0] == "" || phonenoparts[1] == "" || phonenoparts[2] == "")
        { tdWorkPhoneNo.InnerHtml = "&nbsp;&nbsp;-"; }
        else
        {
            tdWorkPhoneNo.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["PhoneNo"]);

        }


        // tdMobileNo.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["MobileNo"]);
        string mobileno = Convert.ToString(DtCandidateData.Rows[0]["MobileNo"]);
        string[] mobparts = mobileno.Split('-');
        if (mobparts[1] == "")
        { tdMobileNo.InnerHtml = "&nbsp;&nbsp;-"; }
        else
        {
            tdMobileNo.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["MobileNo"]);

        }


        //tdGraduationID.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["GraduationID"]);
        tdGraduation.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["GraduationType"]);
        //tdSpecialization.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["Specialisation"]);
        tdSpecialization.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["Specialisation"]);
        tdUniversity.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["University"]);
        //tdQualificationStatusID.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["QualificationStatusID"]);
        tdStatus.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["QualificationStatus"]);
        tdProCurrentRole.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["ProCurrentRole"]);
        tdProExperience.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["ProExperience"]);
        tdProfQualitative1.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["ProfQualitative1"]);
        tdProfQualitative2.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["ProfQualitative2"]);
        tdProfQualitative3.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["ProfQualitative3"]);
        tdProfQualitative4.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["ProfQualitative4"]);


        //tdCompanyName.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["CompanyName"]);
        //txtStatusID.Text = Convert.ToString(DtCandidateData.Rows[0]["StatusID"]);

        string strProfilePath = Convert.ToString(DtCandidateData.Rows[0]["ProfilePicturePath"]);
        if (strProfilePath == "")
        {
            Image1.ImageUrl = "~/Images/Unknown2.jpg";

        }
        else
        {
            Image1.ImageUrl = DocumentPath + '/' + strProfilePath;
        }
        tdCandidateLanguageDetail.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["LanguageList"]);
        txtStatusID.Text = Convert.ToString(DtCandidateData.Rows[0]["StatusID"]);
        tdRegistrationNo.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["RegistrationNo"]);
        string strPath = string.Empty;
        string strCvTitle = Convert.ToString(DtCandidateData.Rows[0]["CVPath"]);
        string strAuthority = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpContext.Current.Request.ApplicationPath;
        string strUCvpath = CVPath.Remove(0, 1);
        string filecheck = ResolveUrl("~" + strUCvpath + strCvTitle);
        strPath = strAuthority + strUCvpath + strCvTitle; //"~" + strUCvpath + strCvTitle
        strPath = strPath.Replace("http://", "https://");

        if (File.Exists(Server.MapPath("~" + strUCvpath + strCvTitle)) == true)
        {
            tdUploadResume.InnerHtml = "<a href=javascript:onClickLink('" + strPath + "')>" + "<b>Click Here</b>" + "</a>";
        }
        else
        {
            tdUploadResume.InnerHtml = "File does not exits";
        }

        if (Convert.ToInt64(DtCandidateData.Rows[0]["PartnerIntID"]) > 0)
        {
            string sPartnetDetails;
            //trCandidatePreference1.Visible = true;
            trCandidatePreference2.Visible = true;

            if (Convert.ToString(DtCandidateData.Rows[0]["AccrediationType"]) == "CANM")
            {
                tdCandidatePreference1.InnerHtml = "Research Lead Only";
                sPartnetDetails = "(Research Lead Only) ";
            }
            else
            {
                tdCandidatePreference1.InnerHtml = "Moderator Only";
                sPartnetDetails = "(Moderator Only) ";
            }
            DataSet objPartnetDs = new UserMasterBAL().GetCandidateDetailsByRegNum(Convert.ToString(DtCandidateData.Rows[0]["ParternerRegistrationNo"]));
            sPartnetDetails = sPartnetDetails + objPartnetDs.Tables[0].Rows[0]["CandidateName"].ToString() + " [" + Convert.ToString(DtCandidateData.Rows[0]["ParternerRegistrationNo"])+ "]";
            tdCandidatePreference2.InnerHtml = sPartnetDetails;
            //
            //tdCandidatePreference2.InnerHtml = Convert.ToString(DtCandidateData.Rows[0]["ParternerRegistrationNo"]);
        }
        else
        {
            if (Convert.ToString(DtCandidateData.Rows[0]["AccrediationType"]) == "CANB")
            {
                trCandidatePreference1.Visible = false;
                trCandidatePreference2.Visible = false;
            }
            else
            {
                trCandidatePreference1.Visible = true;
                tdCandidatePreference1.InnerText = "Alone";
                trCandidatePreference2.Visible = false;
            }
        }
    }
    public void GetUserPaymentDetails(DataTable DtGetUserPaymentDetailsData, DataTable dtAssessorLocalPaymentInfo)
    {
        
        tdCandidateID.InnerHtml = Convert.ToString(DtGetUserPaymentDetailsData.Rows[0]["CandidateID"]);
        tdPaymentAmount.InnerHtml = Convert.ToString(DtGetUserPaymentDetailsData.Rows[0]["PaymentAmount"]);
        tdPaymentDate.InnerHtml = Convert.ToString(DtGetUserPaymentDetailsData.Rows[0]["PaymentDate"]);
        tdTxnId.InnerHtml = Convert.ToString(DtGetUserPaymentDetailsData.Rows[0]["TxnId"]);
        DataSet ds = new PayResponseBAL().GetPaymentforOffline(Convert.ToString(DtGetUserPaymentDetailsData.Rows[0]["CandidateID"]), 0);
        gvCandidatePayment.DataSource = ds;
        gvCandidatePayment.DataBind();

        if (bool.Parse(dtAssessorLocalPaymentInfo.Rows[0]["IsAssessorFeesPaid"].ToString()) == true)
        {
            tdAssPayApproved.InnerHtml = (bool.Parse(dtAssessorLocalPaymentInfo.Rows[0]["IsAssessorFeesPaid"].ToString()) ? "Assessor Payment Confirmed" : "Assessor Payment Confirmation Pending");
            tdAssPayAmount.InnerHtml = Convert.ToString(dtAssessorLocalPaymentInfo.Rows[0]["PaymentAmount"]);
            tdAssPayDate.InnerHtml = Convert.ToString(dtAssessorLocalPaymentInfo.Rows[0]["PaymentDate"]);
            tdAssPayTxnID.InnerHtml = Convert.ToString(dtAssessorLocalPaymentInfo.Rows[0]["TxnId"]);
            ds = new PayResponseBAL().GetPaymentforOffline(Convert.ToString(DtGetUserPaymentDetailsData.Rows[0]["CandidateID"]), 1);
            gvLocalCandidatePayment.DataSource = ds;
            gvLocalCandidatePayment.DataBind();
        }
        else
        {
            trAssessorLocPay1.Visible = false;
            trAssessorLocPay2.Visible = false;
            trAssessorLocPay3.Visible = false;
            trAssessorLocPay4.Visible = false;
            trAssessorLocPay5.Visible = false;
            trAssessorLocPay6.Visible = false;

        }
    }

    private DataSet GetCandidateDetailsDataSet(int cid)
    {
        DataSet objDs = null;
        try
        {

            objDs = new UserMasterBAL().GetCandidatedetailReg(cid);
            string strUserRole = CommonUtility.GetCurrentUserRole();
            GetCandidateData(objDs.Tables[0]);
            GetUsersCompany(objDs.Tables[1]);
            GetCandidateFeeDetails(objDs.Tables[3], objDs.Tables[0]);
            string strStatuaID = txtStatusID.Text.Trim();
            int intStatusID = Convert.ToInt32(strStatuaID);

            if (strStatuaID == "1" || strStatuaID == "2" || strStatuaID == "3" || strStatuaID == "23" || strStatuaID == "26" || strStatuaID == "25" || strStatuaID == "31")
            {
                mainMenu.Items.Remove(mainMenu.Items[1]);

                if (strStatuaID != "31")
                {
                    mainMenu.Items.Remove(mainMenu.Items[1]);
                }
            }
            else
            {
                if (strUserRole == "CANM" || strUserRole == "CANB" || strUserRole == "CANR" || strUserRole == "IQKSAD" || strUserRole == "IQKSMG")
                {
                    if (objDs.Tables[2].Rows.Count > 0)
                    {
                        GetUserPaymentDetails(objDs.Tables[2], objDs.Tables[4]);
                    }
                }
                else
                {
                    mainMenu.Items.Remove(mainMenu.Items[1]);
                }
            }
            //if (intStatusID == 3 )  
            //{
            //}
            if (intStatusID == 1 || intStatusID == 23 || intStatusID == 2 || intStatusID == 26 || strStatuaID == "31")
            {
                trAssesorAllocate.Visible = false;
            }
            else
            {
                if (strUserRole == "IQKSMG" || strUserRole == "IQKSAD" || strUserRole == "SU")
                {
                    trAssesorAllocate.Visible = true;
                }
                else
                {
                    trIqksMgr.Visible = false;
                    trTotalOExpense.Visible = false;
                    trPotalAdminExpenseComment.Visible = false;
                    trPortakAdminExpenseFee.Visible = false;
                    trTotalFee.Visible = false;
                    trDateAllocate.Visible = false;
                    trCurrencyType.Visible = false;

                    trCurrencyRate.Visible = false;
                    trGrandTotal.Visible = false;
                    trModAssFee.Visible = false;
                    trRLAssFee.Visible = false;
                    trAuditorFee.Visible = false;
                    trIExpertFee.Visible = false;
                }

            }
        }
        catch (Exception exception)
        {
            exception.AddErrorToLog(); //throw new ApplicationException(exception.Message);
        }
        return objDs;
    }

    private void GetCandidateFeeDetails(DataTable userDataTable, DataTable usertable1)
    {
        if (userDataTable.Rows.Count > 0)
        {
            trAssesorAllocate.Visible = true;
            foreach (DataRow dr in userDataTable.Rows)
            {
                string userrole = Convert.ToString(dr["RoleName"]);
                switch (userrole)
                {
                    case "RAM": tdModAssessor.InnerHtml = Convert.ToString(dr["AssessorName"]);
                        tdModAssessorFee.InnerHtml = Convert.ToString(dr["FeesAmt"]);
                        trModAss.Visible = true;
                        trModAssFee.Visible = true;
                        break;

                    case "RARL": tdResearchLead.InnerHtml = Convert.ToString(dr["AssessorName"]);
                        tdResearchLeadFee.InnerHtml = Convert.ToString(dr["FeesAmt"]);
                        trRLAss.Visible = true;
                        trRLAssFee.Visible = true;
                        break;

                    case "AD": tdAuditor.InnerHtml = Convert.ToString(dr["AssessorName"]);
                        tdAuditorFee.InnerHtml = Convert.ToString(dr["FeesAmt"]);
                        trAuditor.Visible = true;
                        trAuditorFee.Visible = true;
                        break;

                    case "IE": tdIndustryExpert.InnerHtml = Convert.ToString(dr["AssessorName"]);
                        tdExpertFee.InnerHtml = Convert.ToString(dr["FeesAmt"]);
                        trIExpert.Visible = true;
                        trIExpertFee.Visible = true;
                        break;

                    case "IQKSMG": string IsOutOfExpense = Convert.ToString(dr["IsOutOfExpense"]);
                        //if(IsOutOfExpense =="A")
                        //{
                        //    tdPortalAdminExp.InnerHtml = Convert.ToString(dr["FeeAmt"]);

                        //}
                        //if (IsOutOfExpense == "O")
                        //{
                        //   tdTotalOExpenses.InnerHtml = Convert.ToString(dr["FeesAmt"]);

                        //}
                        switch (IsOutOfExpense)
                        {

                            case "O ": tdTotalOExpenses.InnerHtml = Convert.ToString(dr["FeesAmt"]);
                                break;

                            case "A ": tdPortalAdminExpFee.InnerHtml = Convert.ToString(dr["FeesAmt"]);
                                break;


                            case "": tdIqksManager.InnerHtml = Convert.ToString(dr["FeesAmt"]);
                                break;
                        }

                        break;


                }

            }

            string portalAdmin = Convert.ToString(usertable1.Rows[0]["PortalAdministrationRemark"]);
            if (portalAdmin == "")
            {
                tdPortalAdminExp.InnerHtml = "&nbsp;&nbsp;-";
            }
            else
            {
                tdPortalAdminExp.InnerHtml = Convert.ToString(usertable1.Rows[0]["PortalAdministrationRemark"]);
            }

            tdTotalFee.InnerHtml = Convert.ToString(usertable1.Rows[0]["TotalFees"]);
            string currencyDate = Convert.ToString(usertable1.Rows[0]["CurrencyDate"]);
            if (currencyDate == "")
            {
                tdDateAllocAssess.InnerHtml = "&nbsp;&nbsp;-";

            }
            else
            {
                tdDateAllocAssess.InnerHtml = Convert.ToString(usertable1.Rows[0]["CurrencyDate"]);
            }
            tdCurrencyRate.InnerHtml = Convert.ToString(usertable1.Rows[0]["CurrencyRate"]);
            tdGrandTotalFees.InnerHtml = Convert.ToString(usertable1.Rows[0]["GrossTotalFees"]);
            tdCurrencyType.InnerHtml = Convert.ToString(usertable1.Rows[0]["CurrencType"]);
            if (Convert.ToString(usertable1.Rows[0]["AssessorLocalFeesAmount"]) != "0.00" && Convert.ToString(usertable1.Rows[0]["AssessorLocalFeesAmount"]) != string.Empty)
            {
                trAssessorLocalFeesAmount.Visible = true;
                trAssessorLocalFeesCurrencyType.Visible = true;
                tdAssessorLocalFeesAmount.InnerHtml = Convert.ToString(usertable1.Rows[0]["AssessorLocalFeesAmount"]);
                tdAssessorLocalFeesCurrencyType.InnerHtml = Convert.ToString(usertable1.Rows[0]["AssessorLocalFeesCurrencyType"]);
                tdAssessorLocationFeesAmount.InnerHtml = Convert.ToString(usertable1.Rows[0]["AssessorLocationFeesAmount"]);
                tdAssessorLocation.InnerHtml = Convert.ToString(usertable1.Rows[0]["AssessorLocation"]);
                tdAssessorLocalFees.InnerHtml = Convert.ToString(usertable1.Rows[0]["AssessorLocalFees"]);
            }
            else
            {
                trAssessorLocalFeesAmount.Visible = false;
                trAssessorLocalFeesCurrencyType.Visible = false;
                trAssessorLocationFeesAmount.Visible = false;
                trAssessorLocation.Visible = false;
                trAssessorLocalFees.Visible = false;
            }
                

        }
    }

    private void GetUsersCompany(DataTable DtUsersCompanyData)
    {
        gvUser.DataSource = (DtUsersCompanyData);
        gvUser.DataBind();
    }


    protected void btnNext_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/home/CandidateStatus.aspx");
    }

    protected void mainMenu_MenuItemClick(object sender, MenuEventArgs e)
    {

        int intActiveIndex;
        if (e != null)
        {
            mvwCandidateReg.ActiveViewIndex = Int32.Parse(e.Item.Value);
        }

        intActiveIndex = mvwCandidateReg.ActiveViewIndex;



    }

    //public string getRemoteAddr()
    //{
    //    string UserIPAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    //    if (UserIPAddress == null)
    //    {
    //        UserIPAddress = Request.ServerVariables["REMOTE_ADDR"];
    //    }
    //    return UserIPAddress;
    //}
    //public string getSecureCookie(HttpRequest Request)
    //{

    //    HttpCookie secureCookie = Request.Cookies["vsc"];
    //    if (secureCookie != null)
    //    {
    //        return secureCookie.ToString();
    //    }
    //    else
    //    {
    //        return "";
    //    }


    //}

}